#-*- coding: utf-8 -*-
#  Copyright 2011-2013 wuweihui@cn.ibm.com
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import ConfigParser
import os
import subprocess
import re

config = ConfigParser.RawConfigParser()
config.read('auto.cfg')

def exec_shell(cmd):
   if os.name == 'nt':
      out = os.popen(cmd).read()
   else:
      out = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
   return out

def svn(path, server, user=None, passwd=None):
   re_version = re.compile(r'Checked out revision (\d+)\.|At revision (\d+)\.')
   version = 1
   if not os.path.exists(path):
      cmd = 'svn co %s %s' % (server, path)
   else:
      cmd = 'svn up %s' % path
   if user:
      cmd += ' --username %s' % user
   if passwd:
      cmd += ' --password %s' % passwd
   out = exec_shell(cmd)
   lines = out.splitlines()
   match = re_version.match(lines[-1])
   if match:
      version = match.groups()[0] or match.groups()[1]
   return version
   
def if_svn_updated(path, server, user=None, passwd=None):
   """
wei@wei-VirtualBox:~/svn/CIMTool/CIMCLI$ svn info ~/svn/CIMTool/CIMCLI
Path: ~/svn/CIMTool/CIMCLI
URL: https://9.123.236.75/svn/CIMCLI
Repository Root: https://9.123.236.75/svn/CIMCLI
Repository UUID: 4660df36-0d9c-3142-af3f-c13ce8a930de
Revision: 8
Node Kind: directory
Schedule: normal
Last Changed Author: wei
Last Changed Rev: 8
Last Changed Date: 2011-05-31 17:07:55 +0800 (Tue, 31 May 2011)

   """
   re_version = re.compile(r'Revision:\s+(\d+)')
   version = 0
   if os.path.exists(path):
      out = exec_shell('svn info %s' % path)
      for line in out.splitlines():
         match = re_version.match(line)
         if match:
            version = match.groups()[0]
            break
   newversion = svn(path, server, user, passwd)
   if newversion > version:
      return True
   else:
      return False

def git(path, server):
   pass

def if_git_updated(path, server, user=None, passwd=None):
   """
wei@wei-VirtualBox:~/svn/Git/Hudson$ git pull
Already up-to-date.
wei@wei-VirtualBox:~/svn/Git/Hudson$ 
   """
   curdir = os.getcwd()
   if os.path.exists(path):
      os.chdir(path)
      out = exec_shell('git pull')
      if out.splitlines()[-1] == 'Already up-to-date.':
         return False
   else:
      os.chdir(os.path.split(path)[0])
      cmd = 'git clone %s' % server
      out = exec_shell(cmd)
   os.chdir(curdir)
   return True

def gsa(path, server):
   pass

def check_if_updated(tool, path, server, user=None, passwd=None):
   """
   tool = git,  svn,  gsa
   """
   cmd = 'if_' + tool + '_updated'
   return eval(cmd)()



if __name__ == '__main__':
   print if_svn_updated('/home/wei/svn/CIMTool/CIMCLI', 'https://9.123.236.75/svn/CIMCLI', user='wei', passwd='wwh007')
   print if_git_updated('/home/wei/svn/Git/Hudson', '/home/wei/Hudson')